package demoMod.invincibleOne.cards.invincible;

import basemod.BaseMod;
import basemod.helpers.CardModifierManager;
import basemod.interfaces.OnPlayerTurnStartSubscriber;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.AbstractPower;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.modifiers.ExhaustAtStartOfCombatMod;
import demoMod.invincibleOne.powers.OilStainedPower;

public class FireHazard extends AbstractInvincibleCard implements OnPlayerTurnStartSubscriber {
    public static final String ID = InvincibleOneMod.makeID("FireHazard");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/FireHazard.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.COMMON;
    private static final CardTarget TARGET = CardTarget.SELF;

    private static final int COST = -2;

    public FireHazard() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.modifier = new ExhaustAtStartOfCombatMod();
        this.tags.add(CardTagsEnum.ALERT);
        this.modifier = new ExhaustAtStartOfCombatMod();
        CardModifierManager.addModifier(this, modifier);
        this.baseMagicNumber = this.magicNumber = 3;
    }

    @Override
    public boolean onBattleStart() {
        BaseMod.unsubscribe(this);
        BaseMod.subscribe(this);
        return true;
    }

    @Override
    public void onRemoveFromMasterDeck() {
        super.onRemoveFromMasterDeck();
        BaseMod.unsubscribeLater(this);
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> upgradeMagicNumber(-1);
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {

    }

    @Override
    public void receiveOnPlayerTurnStart() {
        addToBot(
            new AbstractGameAction() {
                @Override
                public void update() {
                    AbstractPlayer p = AbstractDungeon.player;
                    int counter = 0;
                    for (AbstractPower pw : p.powers) {
                        if (pw.type == AbstractPower.PowerType.DEBUFF) {
                            counter++;
                        }
                    }
                    if (counter >= magicNumber)
                    {
                        addToBot(new ApplyPowerAction(p, p, new OilStainedPower(p, counter / magicNumber)));
                    }
                    isDone = true;
                }
            }
        );
    }
}
